
import { _decorator, Button, Component, Label, misc, Node, Sprite } from 'cc';
import { GameData } from '../system/GameData';
import { AppControll, EventName, PopView } from '../manager/AppControll';
import { DataHelper } from '../system/DataHelper';
import EvtManger from '../tools/EvtManger';
import { PopManager } from '../manager/PopManager';
import { SystemMessage } from './SystemMessage';
import { SoundManager } from '../manager/SoundManger';
import { unlockType } from './Skin';
const { ccclass, property } = _decorator;

@ccclass('AutoFight')
export class AutoFight extends Component {
    private mNdBody: Node = null;
    private mNdUpgrade: Node = null;
    private mNdContent: Node = null;
    private mNdConfirm: Node = null;
    private mNdCancel: Node = null;
    private mNdUnlock: Node = null;
    private mNdLevelUp: Node = null;
    private mNdClose: Node = null;
    private mNdCurAutoNum: Node = null;
    private mNdNextAutoNum: Node = null;
    private mIsLock = false;
    protected onLoad(): void {
        this.mNdBody = this.node.getChildByName("body");
        this.mNdUpgrade = this.mNdBody.getChildByName("ndUpgrade");
        this.mNdContent = this.mNdBody.getChildByName("lbContent");
        this.mNdConfirm = this.mNdBody.getChildByName("yesBtn");
        this.mNdCancel = this.mNdBody.getChildByName("noBtn");
        this.mNdUnlock = this.mNdConfirm.getChildByName("label").getChildByName("yes")
        this.mNdLevelUp = this.mNdConfirm.getChildByName("label").getChildByName("levelUp")
        this.mNdClose = this.mNdBody.getChildByName("closeBtn");
        this.mNdCurAutoNum = this.mNdUpgrade.getChildByName("Label-003");
        this.mNdNextAutoNum = this.mNdUpgrade.getChildByName("Label-005");
        this.RegisteredEvent();
        this.InitView();
    }

    RegisteredEvent() {
        this.mNdConfirm.on(Node.EventType.TOUCH_END, this.OnConfirm, this);
        this.mNdCancel.on(Node.EventType.TOUCH_END, this.OnCancel, this);
        this.mNdClose.on(Node.EventType.TOUCH_END, this.OnClose, this);
    }

    InitView() {
        if (GameData.playerData.MGAutomaticAttackLv == 0) {
            this.mNdContent.active = true;
            this.mNdUpgrade.active = false;
            this.mNdLevelUp.active = false;
            this.mNdUnlock.active = true;
        } else {
            if (DataHelper.GetAutoSpeedByLv(GameData.playerData.MGAutomaticAttackLv + 1)) {
                this.mNdContent.active = false;
                this.mNdUpgrade.active = true;
                this.mNdLevelUp.active = true;
                this.mNdUnlock.active = false;
                this.mNdConfirm.getComponent(Sprite).grayscale = false;
                this.mNdConfirm.getComponent(Button).interactable = true;
                let attackInterval = 0;
                let arr = GameData.playerData.skin;
                arr.forEach(id => {
                    let config = DataHelper.GetSkin_configById(id);
                    switch (config.unlockAttrType) {
                        case unlockType.AttackInterval:
                            attackInterval += config.unlockNum;
                            break;
                    }
                })
                this.mNdCurAutoNum.getComponent(Label).string = (DataHelper.GetAutoSpeed().attributeNum - attackInterval) + "";
                this.mNdNextAutoNum.getComponent(Label).string = (DataHelper.GetAutoSpeedByLv(GameData.playerData.MGAutomaticAttackLv + 1).attributeNum - attackInterval) + "";
            } else {
                this.mNdContent.active = true;
                this.mNdUpgrade.active = false;
                this.mNdLevelUp.active = true;
                this.mNdUnlock.active = false;
                this.mNdContent.getComponent(Label).string = "已满级"
                this.mNdConfirm.getComponent(Sprite).grayscale = true;
                this.mNdConfirm.getComponent(Button).interactable = false;
            }
        }
    }

    OnConfirm() {
        AppControll.ShelvedTime = 0;
        if (this.mIsLock) return;
        if (!this.mNdConfirm.getComponent(Button).interactable) return
        this.mIsLock = true;
        this.scheduleOnce(() => {
            this.mIsLock = false;
        }, 0.5);
        GameData.isPause = true;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        AppControll.Platform.ShowRewardVideoAd(this, (bool: boolean, str: string) => {
            this.mIsLock = false;
            GameData.isPause = false;
            if (!bool) {
                PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, (node) => {
                    node.getComponent(SystemMessage).InitContent(str);
                })
                return;
            }
            AppControll.Platform.SendBuryPoint(100006);
            if (DataHelper.GetAutoSpeedByLv(GameData.playerData.MGAutomaticAttackLv + 1) && bool) {
                GameData.playerData.MGAutomaticAttackLv += 1;
                this.node.active = false;
                EvtManger.do(EventName.automaticUpgrade);
            }
        })
    }

    OnCancel() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (this.mIsLock) return;
        this.node.active = false;
    }

    OnClose() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (this.mIsLock) return;
        this.node.active = false;
    }

    start() {
    }
}
